package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;

/*一个事务进行多个操作

 */
public class JDBCDemo13 {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection()){
            //先关闭自动提交事务功能
            connection.setAutoCommit(false);
            String sql = "INSERT INTO userinfo(username,password,nickname,age) VALUES (?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,"张三");
            ps.setString(2,"444");
            ps.setString(3,"阿三");
            ps.setInt(4,33);
            ps.executeUpdate();

            ps.setString(1,"李四");
            ps.setString(2,"555");
            ps.setString(3,"老四");
            ps.setInt(4,44);
            ps.executeUpdate();
            System.out.println("添加成功");
            connection.commit();
            //connection.rollback();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
